package myDaos;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import model.entities.ChubCountryMapping;

@Stateless
public class MappingDao {

	@PersistenceContext(unitName = "UnileverProcedure")
    private EntityManager em;
	
	public EntityManager getEntityManager() {
        return em;
    }
	
	
	public List<ChubCountryMapping> getMappingsByFilter(String country,String distributer){
		List<ChubCountryMapping> dlist=null;
		try {
			StringBuffer queryBiffer = new StringBuffer();
			queryBiffer.append(" SELECT m FROM ChubCountryMapping m WHERE 1=1 "); 
		    	
			if(country!=null && !country.trim().equals(""))
    	    {
				queryBiffer.append(" and m.cmmCountryCode = :country") ;	
    	    }
			
			if(distributer!=null && !distributer.trim().equals(""))
    	    {
				queryBiffer.append(" AND m.cmmDistCode = :distributer") ;	
    	    }

			
			Query q = getEntityManager().createQuery(queryBiffer.toString());
 
			if(country!=null && !country.trim().equals("")){q.setParameter("country", country);}
			if(distributer!=null && !distributer.trim().equals("")){q.setParameter("distributer", distributer);}
			
			dlist = q.getResultList();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dlist;
	}
	
}
